package com.example.liao.qrcodescanning;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.google.zxing.encoding.EncodingHandler;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_scan;
    private Button btn_createCode;
    private TextView tv_show;
    private EditText et_code;
    private ImageView iv_icon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn_scan = findViewById(R.id.btn_scan);
        btn_createCode = findViewById(R.id.btn_createCode);

        btn_scan.setOnClickListener(this);
        btn_createCode.setOnClickListener(this);

        tv_show = findViewById(R.id.tv_show);
        et_code = findViewById(R.id.et_code);
        iv_icon = findViewById(R.id.iv_icon);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_scan:

                break;
            case R.id.btn_createCode:
                try {
                    String s = et_code.getText().toString();

                    //生成二维码  (参数一字符串  参数二 二维码图片的大小/px)
                    Bitmap qrCode = EncodingHandler.createQRCode(s, 400);
                    iv_icon.setImageBitmap(qrCode);

                } catch (WriterException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
